package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;

import controller.Main;
import model.ABook;
import model.Contact;
import model.File;


public class ContactFrame extends JFrame{
	

	private JMenuBar menuBar;
	private JMenu test1;
	private JMenu test2;
	private JMenuItem item1;
	private JMenuItem item2;
	private JMenuItem item3;
	private JMenuItem item4;
	private JPanel container = new JPanel();
	private JTextField addField1;
	private JTextField addField2;
	private JTextField addField3;
	private JTextField addField4;
	private JTextField addField5;
	private JTextField addField6;
	private JTextField addField7;
	private JTextField addField8;
	private JTextField addField9;
	private JComboBox combo;
	
	static JPanel contact1; 
	static JPanel contactPanel1;
	static JPanel buttons;
	static JPanel panEcran;
	static ContactTable newContentPane;
	
	private static JLabel ecran = new JLabel();
	private static JLabel ecran1 = new JLabel();
	private static JLabel ecran2 = new JLabel();
	private static JLabel ecran3 = new JLabel();
	private static JLabel ecran4 = new JLabel();
	private static JLabel ecran5 = new JLabel();
	private static JLabel ecran6 = new JLabel();
	private static JLabel ecran7 = new JLabel();
	private static JLabel ecran8 = new JLabel();
	
	private JButton Submit;
	private static JButton Modify;
	private static JButton Delete;
	static Dimension dim = new Dimension(20,20);

	
	public ContactFrame(ABook a) {
		super("ABook");		
		this.initComposant();		
		this.render();
	}
	
	private void initComposant(){
		initMenu();
		initContact();
		initTab();
		int i = ContactTable.getTable().getSelectedRowCount();
		initScreen(i,
				Main.tab.get(i).getFirstName(),
				Main.tab.get(i).getLastName(),
				Main.tab.get(i).getHomeAddress(),
				Main.tab.get(i).getCompanyAddress(),
				Main.tab.get(i).getPersonalPhone(),
				Main.tab.get(i).getHomePage(),
				Main.tab.get(i).getEmail(),
				Main.tab.get(i).getPhoto(),
				Main.tab.get(i).getGroup());
		//creation of a container 
		getContainer().setLayout(new BorderLayout());
		getContainer().add(panEcran, BorderLayout.WEST);
		getContainer().add(contact1, BorderLayout.SOUTH);
	    getContainer().add(contactPanel1, BorderLayout.EAST);
	    getContainer().setVisible(true);
	}
		
	public JPanel initContact(){
		
		contactPanel1 = new JPanel();
		contactPanel1.setLayout(new GridLayout(11,2));
		
				//field firstName
				addField1 = new JTextField(10);
				addField1.setEditable(true);
				contactPanel1.add(new JLabel("First Name:"));
				contactPanel1.add(addField1);
	  
				//field lastName
				addField2 = new JTextField(10);
				addField2.setEditable(true);
				contactPanel1.add(new JLabel("Last Name:"));
				contactPanel1.add(addField2);
				
				//field home address
				addField3 = new JTextField(10);
				addField3.setEditable(true);
				contactPanel1.add(new JLabel("Home Address:"));
				contactPanel1.add(addField3);
						
				//field company address
				addField4 = new JTextField(10);
				addField4.setEditable(true);
				contactPanel1.add(new JLabel("Company Address:"));
				contactPanel1.add(addField4);
				
				//field Personal phone
				addField5 = new JTextField(10);
				addField5.setEditable(true);
				contactPanel1.add(new JLabel("Personal Phone:"));
				contactPanel1.add(addField5);
				
				//field Professional phone
				addField6 = new JTextField(10);
				addField6.setEditable(true);
				contactPanel1.add(new JLabel("Professional Phone:"));
				contactPanel1.add(addField6);
				
				//field home page
				addField7 = new JTextField(10);
				addField7.setEditable(true);
				contactPanel1.add(new JLabel("Home Page:"));
				contactPanel1.add(addField7);
				
				//field Email
				addField8 = new JTextField(10);
				addField8.setEditable(true);
				contactPanel1.add(new JLabel("Email:"));
				contactPanel1.add(addField8);
				
				//field Photo
				addField9 = new JTextField(10);
				addField9.setEditable(true);
				contactPanel1.add(new JLabel("Photo:"));
				contactPanel1.add(addField9);
				
				String[] tab = {"Family", "Friend","Work"};
				combo = new JComboBox(tab);
				contactPanel1.add(new JLabel("Group:"));
				contactPanel1.add(combo);
				/*add(contactPanel1);
				contactPanel1.setBounds(400, 400, 1200, 1200);*/
				
				Submit = new JButton("Submit");
				contactPanel1.add(Submit);
				Submit.addActionListener(new addNewContact());
				return contactPanel1;
		
	}
	/**
	 * Initializing the table
	 * @return
	 */
	public JPanel initTab(){
		contact1 = new JPanel();
		newContentPane = new ContactTable();
		contact1.add(newContentPane);
		getContainer().setVisible(false);
		return contact1;
	}
	/**
	 * Initializing the screen
	 * @param firstName
	 * @param lastName
	 * @param homeAddress
	 * @param companyAddress
	 * @param personalPhone
	 * @param homePage
	 * @param email
	 * @param photo
	 * @param group
	 * @return
	 */
	public static JPanel initScreen(final int i, String firstName,String lastName,String homeAddress,String companyAddress,String personalPhone,String homePage, String email, String photo, String group){
		
		//Screen to see contact list
	    ecran = new JLabel(" First Name: " + firstName); 
	    ecran1 = new JLabel(" Last Name: " + lastName);
	    ecran2 = new JLabel(" Home Address: " + homeAddress);
	    ecran3 = new JLabel(" Company Address: " + companyAddress);
	    ecran4 = new JLabel(" Personal Phone: " + personalPhone);
	    ecran5 = new JLabel(" Home Page: " + homePage);
	    ecran6 = new JLabel(" Email: " + email);
	    ecran7 = new JLabel(" Photo: " + photo);
	    ecran8 = new JLabel(" Group: " + group);
	    Modify = new JButton("Modify");
	    Delete = new JButton("Delete");
	    Modify.setPreferredSize(dim);
	    Delete.setPreferredSize(dim);
	    panEcran = new JPanel();
	    //action listener
	    Delete.addActionListener(new ActionListener(){
	    	public void actionPerformed(ActionEvent arg0) {
		        Main.tab.remove(i);
		        System.out.println(Main.tab);
		        try {
					File.writeAll();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		      }        
		    });
	    Modify.addActionListener(new ActionListener(){
	    	public void actionPerformed(ActionEvent arg0) {
		        System.exit(0);
		      }        
		    });
	    

	    panEcran.setPreferredSize(new Dimension(600, 200));
	    panEcran.setLayout(new GridLayout(11,2));
	    panEcran.add(ecran);
	    panEcran.add(ecran1);
	    panEcran.add(ecran2);
	    panEcran.add(ecran3);
	    panEcran.add(ecran4);
	    panEcran.add(ecran5);
	    panEcran.add(ecran6);
	    panEcran.add(ecran7);
	    panEcran.add(ecran8);
	    panEcran.add(Modify);
	    panEcran.add(Delete);
	   // add(panEcran);
	    panEcran.setBorder(BorderFactory.createLineBorder(Color.black));
	   panEcran.setVisible(true);
		return panEcran;
	}
	/**
	 * Initialising the menu bar
	 */
	private void initMenu(){
		
	    menuBar = new JMenuBar();
	    test1 = new JMenu("Files");
	    test2 = new JMenu("Group");
	    item1 = new JMenuItem("Quit");
	    item2 = new JMenuItem("Family");
	    item3 = new JMenuItem("Friend");
	    item4 = new JMenuItem("Work");
	       

	    item1.addActionListener(new ActionListener(){
	      public void actionPerformed(ActionEvent arg0) {
	        System.exit(0);
	      }        
	    });
	    item2.addActionListener(new ActionListener(){
		      public void actionPerformed(ActionEvent arg0) {
		        System.exit(0);
		      }        
		    });
	    item3.addActionListener(new ActionListener(){
		      public void actionPerformed(ActionEvent arg0) {
		        System.exit(0);
		      }        
		    });
	    item4.addActionListener(new ActionListener(){
		      public void actionPerformed(ActionEvent arg0) {
		        System.exit(0);
		      }        
		    });
	    test1.setMnemonic('F');
	    this.test1.add(item1);
	    test2.setMnemonic('G');
	    this.test2.add(item2);
	    this.test2.add(item3);
	    this.test2.add(item4);

	    this.menuBar.add(test1);
	    this.setJMenuBar(menuBar);
	    this.setVisible(true);
	    
	    this.menuBar.add(test2);
	    this.setJMenuBar(menuBar);
	    this.setVisible(true);

	  }
	public JPanel getContainer() {
		return container;
	}

	public void render(){
		this.setSize(900,700);
		this.setTitle("ABook");
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);		
		this.setLocationRelativeTo(null);	
		this.setContentPane(getContainer());
		this.setVisible(true);
	}
	public void setContainer(JPanel container) {
		this.container = container;
	}
	class addNewContact implements ActionListener{
	public void actionPerformed(ActionEvent arg0) {
		Contact c = new Contact(addField1.getText(),addField2.getText(),addField3.getText(),addField4.getText(),addField5.getText(),addField7.getText(),addField8.getText(),addField9.getText(),(String) combo.getSelectedItem());
		ABook.add(c);
		try {
			File.write(addField1.getText(),addField2.getText(),addField3.getText(),addField4.getText(),addField5.getText(),addField7.getText(),addField8.getText(),addField9.getText(),(String) combo.getSelectedItem());
		} catch (IOException e) {
			e.printStackTrace();
		}
		//delete field
		addField1=null;
		addField2=null;
		addField3=null;
		addField4=null;
		addField5=null;
		addField6=null;
		addField7=null;
		addField8=null;
		addField9=null;
		combo=null;
		getContainer().setVisible(false);
		initComposant();
}}
	
	class deleteContact implements ActionListener{
		public void actionPerformed(ActionEvent arg0){
			
		}
	}
	
	class showList implements ActionListener{
	public void actionPerformed(ActionEvent arg0) {
		for(int i=0; i < Main.tab.size();i++){
			ecran.setText("<html>" + Main.tab.get(i).getFirstName() + " " + Main.tab.get(i).getLastName() 
					+ " " + Main.tab.get(i).getHomeAddress() + " " + Main.tab.get(i).getCompanyAddress() 
					+ " " + Main.tab.get(i).getPersonalPhone() + " " + Main.tab.get(i).getHomePage() 
					+ " " + Main.tab.get(i).getEmail() + " " + Main.tab.get(i).getPhoto() + " " + Main.tab.get(i).getGroup() + "</html>");
		}
	}

	}
	

		   
		
	}
		